#include "ScrollPanel2.h"
#include "base/UITheme.h"
#include "base/Utility.h"
//
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));
bool ScrollBar::OnInit()
{
	//Control::OnInit();
	return true;
}
//
void ScrollBar::OnDraw()
{
	if (orient == ScrollBarOrient::Vertical) {
		float y = pos.y.val + size.y.val * percent;
		UITheme::GetInst().DrawButtonBK(ButtonState::Normal, pos.x.val, pos.y.val, size.x.val, size.y.val, zIndex.val);
		UITheme::GetInst().DrawButtonBK(ButtonState::Hover, pos.x.val, y, 20, 20, zIndex.val + 0.3);
	}
	else {
		float x = pos.x.val + size.x.val * percent;
		UITheme::GetInst().DrawButtonBK(ButtonState::Normal, pos.x.val, pos.y.val, size.x.val, size.y.val, zIndex.val);
		UITheme::GetInst().DrawButtonBK(ButtonState::Hover, x, pos.y.val, 20, 20, zIndex.val + 0.3);
	}
	DrawBorder();
}
//
void ScrollBar::onMouseButtonUp(int button, int x, int y)
{
	//pos.x.val, y, 50, 50
	if (isMouseDown) {
		isMouseDown = false;
	}
}
//
void ScrollBar::onMouseButtonDown(int button, int x, int y)
{
	//
	if (HitTest(vec2(x, y))) {
		isMouseDown = true;
	}
}
//
void ScrollBar::onMouseMove( int x, int y)
{
	if(!isMouseDown) return;
	if (orient == ScrollBarOrient::Vertical) {
		percent = ((float)y - pos.y.val) / size.y.val;
	}
	else {
		percent = ((float)x - pos.x.val) / size.x.val;
	}
	printf("x:%d,y:%d,percent:%.2f \r\n",x, y, percent);
	//cout<<percent<<endl;
}

bool ScrollPanel2::OnInit()
{
	Control::OnInit();
	scrollBar = new ScrollBar;
	//scrollBar->parent = this; //std::make_shared<Control>(this);
	scrollBar->rootWindow = this->rootWindow;
	return true;
}

void ScrollPanel2::OnDraw()
{
	glColor3f(1.,1.,1.);
	UITheme::GetInst().DrawButtonBK(ButtonState::Hover, pos.x.val, pos.y.val, size.x.val, size.y.val, zIndex.val);
	if (scrollBar != nullptr) {
		scrollBar->zIndex.val = this->zIndex.val + 1;
		if (scrollBar->orient == ScrollBarOrient::Horizental) {
			scrollBar->pos.x.val = pos.x.val;
			scrollBar->pos.y.val = pos.y.val + size.y.val - 20;
			scrollBar->size.x.val = size.x.val;
			scrollBar->size.y.val = 20;
		}
		else {
			scrollBar->pos.x.val = pos.x.val + size.x.val - 20;
			scrollBar->pos.y.val = pos.y.val;
			scrollBar->size.x.val = 20;
			scrollBar->size.y.val = size.y.val;
		}
		scrollBar->OnDraw();
	}
	for (int i = 0; i < childs.size(); i++) {
		childs[i].second->pos.x.val = pos.x.val;
		childs[i].second->pos.y.val = pos.y.val;
		childs[i].second->Draw();
	}
}

void ScrollPanel2::onMouseButtonUp(int button, int x, int y)
{
	scrollBar->onMouseButtonUp(button,x,y);
}

void ScrollPanel2::onMouseButtonDown(int button, int x, int y)
{
	scrollBar->onMouseButtonDown(button, x, y);
}

void ScrollPanel2::onMouseMove( int x, int y)
{
	scrollBar->onMouseMove( x, y);
}

bool ScrollPanel2::SetAttribute(const char* attrName, const char* attrVal)
{
	if (attrName == nullptr) return false;
	if (Control::SetAttribute(attrName, attrVal)) {
		return true;
	}
	if (strcmp(attrName, "scrollorient") == 0) {
		std::string sAttrVal = attrVal;
		ToLowerStr(sAttrVal);
		if (strcmp(sAttrVal.data(), "horizental") == 0) {
			scrollBar->orient = Horizental;
		}
		else {
			scrollBar->orient = Vertical;
		}
		return true;
	}
	return false;
}
Control* ScrollPanel2::CreateControl(std::string sControlClassName)
{
	Control* ctrl = dynamic_cast<Control*>(BlueprintScriptLibrary::BlueprintDataPool::GetInstance()->createInstFuncs[sControlClassName]());
	ctrl->rootWindow = rootWindow;
	ctrl->parent = std::shared_ptr<Control>(this);
	return ctrl;
}
void ScrollPanel2::ParseXmlElement(TiXmlElement* ele)
{
	OnInit();
	Control::ParseXmlElement(ele);
	if (strcmp(ele->Value(), "ScrollPanel2") == 0)
	{
		auto& vattr = Utility::GetTiXmlElementAttributes(ele);
		for (int i = 0; i < vattr.size(); i++) {
			SetAttr(vattr[i].data());
		}
		ele = ele->FirstChildElement();
		while (ele)
		{
			Control* ctrl = CreateControl(ele->Value());
			ctrl->DoParseXmlElement(ele);
			ctrl->rootWindow = this->rootWindow;
			TString tstr = ctrl->name.val.data();

			TiXmlAttribute* ta = ele->FirstAttribute();
			while (ta)
			{
				ctrl->SetAttribute(ta->Name(), ta->Value());
				ta = ta->Next();
			}

			if (ctrl)
			{
				childs.push_back(std::make_pair(tstr, ctrl));
			}
			ele = ele->NextSiblingElement();
			ctrl->OnInit();
		}
	}
}
